package pinboard.dataLayer.potocol.op;

import com.google.gson.Gson;

import java.util.HashMap;

import pinboard.dataLayer.CommHelper;
import pinboard.dataLayer.potocol.instructFormat.Notification;
import pinboard.dataLayer.potocol.instructFormat.notification.AddManagerResult;
import pinboard.dataLayer.potocol.instructFormat.notification.CloseMeetingResult;
import pinboard.dataLayer.potocol.instructFormat.notification.KickedResult;
import pinboard.dataLayer.potocol.instructFormat.notification.RemoveManagerResult;
import pinboard.dataLayer.potocol.instructFormat.notification.ServerForwardResult;
import pinboard.dataLayer.potocol.instructFormat.response.ExitMeetingResult;
import pinboard.dataLayer.potocol.instructFormat.response.JoinMeetingResult;
import pinboard.dataLayer.potocol.instructFormat.notification.UpdateMeetingResult;

class NotificationDecoder {
    private static HashMap<Integer, Class<?>> nTypeMap = new HashMap<>();

    static {
        nTypeMap.put(5, JoinMeetingResult.class);//他人加入會議
        nTypeMap.put(6, ExitMeetingResult.class);//他人离开会议
        nTypeMap.put(10, CloseMeetingResult.class);//TODO 会议被关闭
        nTypeMap.put(11, ServerForwardResult.class);//TODO 服務器轉發協議為二進制格式
        //nTypeMap.put(15, loginElsewhere.class);//在另外一台设备登陆 2017-1-14作废
        nTypeMap.put(17, KickedResult.class);//TODO 被踢出会议
        nTypeMap.put(18, RemoveManagerResult.class);//TODO 移除中控
        nTypeMap.put(19, AddManagerResult.class);//TODO 添加中控
        nTypeMap.put(21, UpdateMeetingResult.class);//更新會議狀態，會議成員信息（包含權限，頁碼）
    }

    public static Notification decode(Notification notification, String msgStr) {
        Gson gson = new Gson();
        if (nTypeMap.containsKey(notification.nType)) {
            Class<?> subClassName = nTypeMap.get(notification.nType);
            Object result = gson.fromJson(msgStr, subClassName);
            return CommHelper.as(result, Notification.class);
        }
        return notification;
    }

    public static void filter(Notification input, InstructClientDecodeListener listener) {
        //input.nType //TODO 其他nType的listener事件
        if (input.nType == 21) {
            listener.onUpdateMeeting((UpdateMeetingResult) input);
        }
    }
}
